<?php

namespace App\Console\Commands\Shop;

use App\Kuafu\Activity\Model\ActivityImageMappingModel;
use App\Remote\Tiger\Common\CommonRemote;
use Illuminate\Console\Command;
use Kuafu\File\FileUtil;

class InitActivityImageMappingsShortLinkUrl extends Command
{
    protected $signature = 'shop:initActivityImageMappingsShortLinkUrl';

    protected $description = '初始化私域活动物料素材链接转换短链';

    public function handle()
    {
        ActivityImageMappingModel::query()
                                 ->where("type", ActivityImageMappingModel::TYPE_MATERIAL_COMPRESS)
                                 ->chunkById(100, function ($activityImageMappingModel) {
                                     foreach ($activityImageMappingModel as $activityImageMapping) {
                                         if (strpos($activityImageMapping->imageUrl, 'export/rms') !== false) {// 源链接

                                             // 生成短链
                                             $oldUrl       = FileUtil::domain() . '/' . $activityImageMapping->imageUrl;
                                             $shortLinkUrl = CommonRemote::generateShortLinkUrl($oldUrl)['shortUrl'] ?? '';

                                             $activityImageMapping->imageUrl = $shortLinkUrl;
                                             $activityImageMapping->save();

                                             dump("生成短链: activityId:{$activityImageMapping->activityId} oldUrl:{$oldUrl} shortLinkUrl:{$shortLinkUrl}");
                                         }
                                     }
                                 });
    }

}
